package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.domain.Templet;
import com.zcxy360.sandtable.service.TempletService;

/**
 * 模板接口
 * @author 盛亚辉
 *
 */
@RequestMapping("templet")
@RestController
public class TempletController {

	@Autowired
	private TempletService templetService;
	
	/**
	 * 获取模板
	 * @param templetId 模板id
	 * @param deep 0代表基本数据,1代表扩展数据,2代表级联数据,3代表扩展及级联数据
	 * @return 模板对象
	 */
	@RequiresAuthentication
	@GetMapping("{templetId}")
	 public Object getByTempletId(@PathVariable("templetId") Integer templetId,@RequestParam(defaultValue="0") Integer deep) {
		 return templetService.getByTempletId(templetId,deep);
	 }
	
	/**
	 * 获取模板集合
	 * @param templet 模板字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param sign 查询标识
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 模板集合
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@GetMapping
	public Object list(Templet templet,String key,String orderByClause,Integer sign,@RequestParam(defaultValue="0")Integer deep,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return templetService.list(templet,key,orderByClause,sign,deep,pageNum,pageSize,navigatePages);
	}
	
	
	/**
	 * 新增模板
	 * @param templet 模板字段
	 * @param planIds 教案id数组
	 * @param videoIds 视频id数组
	 * @param drawingIds 图纸id数组
	 * @param problemIds 问题id数组
	 * @return 模板对象
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PostMapping
	public Object post(Templet templet,int[] planIds,int[] videoIds,int[] drawingIds,int[] problemIds ) {
		return templetService.post(templet,planIds,videoIds,drawingIds,problemIds);
	}
	
	/**
	 * 更新模板
	 * @param templet 模板字段
	 * @param planIds 教案id数组
	 * @param videoIds 视频id数组
	 * @param drawingIds 图纸id数组
	 * @param problemIds 问题id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PutMapping
	public Object update(Templet templet,int[] planIds,int[] videoIds,int[] drawingIds,int[] problemIds) {
		return templetService.update(templet,planIds,videoIds,drawingIds,problemIds);
	}
	
	/**
	 * 删除模板
	 * @param templetIds 模板id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@DeleteMapping("{templetIds}")
	public Object delete(@PathVariable("templetIds") int[] templetIds) {
		return templetService.delete(templetIds);
	}
	
	/**
	 * 检查名称
	 * @param name 模板名称
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@GetMapping("checkName")
	public Object checkName(@RequestParam("name") String name) {
		return templetService.checkName(name);
	}
	
}
